import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Bluebooth {

	public static void main(String[] args) {
		final String path="/home/homeassistant/.homeassistant/tts";
		//final String path="C:\\Users\\Public\\Music\\Sample Music";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat timeSdf=new SimpleDateFormat("HH:mm:ss");
		String strDate=timeSdf.format(new Date());
		int strDateH = Integer.parseInt(strDate.substring(0, 2));
		int strDateM = Integer.parseInt(strDate.substring(3, 5));
		int strDateS = Integer.parseInt(strDate.substring(6,8));
		String holiday=sdf.format(new Date());
		int flag=getHolid(holiday);
		if((flag==0 && strDateH==7 && strDateM==10) || (flag==1 && strDateH==8 && strDateM==30) || (flag==2 && strDateH==8 && strDateM==30)){
			File dir=new File(path);
			File[] files=dir.listFiles();
			for (File file : files) {
				if(file.getName().endsWith("mp3")){
					System.out.println("找到mp3"+file.getName()+",最后修改日期"+sdf.format(new Date(file.lastModified())));
					if(sdf.format(new Date()).equals(sdf.format(new Date(file.lastModified())))){
						//����
						String cmd="/usr/bin/env TERM=xterm mplayer -volume 50 "+file.getAbsolutePath();
						try {
							Process process  =Runtime.getRuntime().exec(cmd);
							LineNumberReader br = new LineNumberReader(new InputStreamReader(process.getInputStream()));  
			                StringBuffer sb = new StringBuffer();  
			                String line;  
			             while ((line = br.readLine()) != null) {  
			                    sb.append(line).append("\n");  
			                }
			             System.out.println(sb.toString());
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						return;
					}
				}
			}
		}
		

	}
	
	public static int getHolid(String date){
		 try {
	            String urlNameString = "http://tool.bitefu.net/jiari/?d=" + date;
	            URL realUrl = new URL(urlNameString);
	            // 打开和URL之间的连接
	            URLConnection connection = realUrl.openConnection();
	            // 设置通用的请求属性
	            connection.setRequestProperty("accept", "*/*");
	            connection.setRequestProperty("connection", "Keep-Alive");
	            // 建立实际的连接
	            connection.connect();
	            // 定义 BufferedReader输入流来读取URL的响应
	            BufferedReader in  = new BufferedReader(new InputStreamReader(
	                    connection.getInputStream()));
	            String line;
	            String result="";
	            while ((line = in.readLine()) != null) {
	                result += line;
	            }
	            return Integer.parseInt(result);
	        } catch (Exception e) {
	            return 3;
	        }
	}

}
